using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GuavaData
{
    public partial class GuavaEditorForm : Form
    {
        public GuavaEditorForm()
        {
            InitializeComponent();
            foreach (GuavaData.Domain.GNumericDomains gnbd in Enum.GetValues(typeof(GuavaData.Domain.GNumericDomains))) this.cmbNumericType.Items.Add(gnbd);
            this.cmbNumericType.SelectedItem = 0;
        }

        public Domain.Domain BaseDomain
        {
            get
            {
                if (this.rBoolean.Checked)
                {
                    return new GuavaData.Domain.Boolean();
                }
                if (this.rString.Checked)
                {
                    int i;
                    if (this.txtMaximumLength.Text.Length > 0 && Int32.TryParse(this.txtMaximumLength.Text, out i)) return new GuavaData.Domain.String(i);
                    return new GuavaData.Domain.String();
                }
                if (this.rLookup.Checked)
                {
                    return new GuavaData.Domain.Lookup(this.txtTableName.Text, this.txtView.Text);
                }
                if (this.rDateTime.Checked)
                {
                    return new GuavaData.Domain.DateTime();
                }
                if (this.rEnumerated.Checked)
                {
                    return new GuavaData.Domain.Enumerated(this.txtEnumItems.Lines);
                }
                if (this.rNumeric.Checked)
                {
                    object min = null;
                    object max = null;
                    Domain.Domain v;
                    switch ((Domain.GNumericDomains)this.cmbNumericType.SelectedItem)
                    {
                        case Domain.GNumericDomains.FixedPoint:
                            v = new Domain.Decimal();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.Decimal((decimal?) min, (decimal?) max);

                        case Domain.GNumericDomains.FloatingPointDouble:
                            v = new Domain.Double();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.Double((double?)min, (double?)max);

                        case Domain.GNumericDomains.FloatingPointSingle:
                            v = new Domain.Single();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.Single((float?)min, (float?)max);

                        case Domain.GNumericDomains.Integer1Byte:
                            v = new Domain.TinyInteger();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.TinyInteger((byte?)min, (byte?)max);

                        case Domain.GNumericDomains.Integer2Byte:
                            v = new Domain.SmallInteger();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.SmallInteger((short?)min, (short?)max);

                        case Domain.GNumericDomains.Integer4Byte:
                            v = new Domain.Integer();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.Integer((int?)min, (int?)max);

                        case Domain.GNumericDomains.Integer8Byte:
                            v = new Domain.BigInteger();
                            if (this.txtMaxValue.Text == "" && this.txtMinValue.Text == "") return v;
                            try
                            {
                                min = v.ConvertString(this.txtMinValue.Text);
                                max = v.ConvertString(this.txtMaxValue.Text);
                            }
                            catch (GuavaData.Domain.DomainConversionException dce)
                            {
                                MessageBox.Show(dce.Message);
                                return null;
                            }
                            return new Domain.BigInteger((long?)min, (long?)max);
                    }
                }
                return null;
            }
            set
            {
                if (value is Domain.String)
                {
                    this.rString.Checked = true;
                    Domain.String v = value as Domain.String;
                    if (v.Width > 0) this.txtMaximumLength.Text = v.Width.ToString();
                    return;
                }
                if (value is Domain.Enumerated)
                {
                    this.rEnumerated.Checked = true;
                    Domain.Enumerated v = value as Domain.Enumerated;
                    this.txtEnumItems.Lines = v.Items.ToArray();
                    return;
                }
                if (value is Domain.Boolean)
                {
                    this.rBoolean.Checked = true;
                    return;
                }
                if (value is Domain.DateTime)
                {
                    this.rDateTime.Checked = true;
                    return;
                }
                if (value is Domain.Lookup)
                {
                    this.rLookup.Checked = true;
                    Domain.Lookup v = value as Domain.Lookup;
                    this.txtTableName.Text = v.TableName;
                    this.txtView.Text = v.View;
                    return;
                }
                if (value is Domain.Single)
                {
                    this.rNumeric.Checked = true;
                    Domain.Single v = value as Domain.Single;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.FloatingPointSingle;
                    return;
                }
                if (value is Domain.Double)
                {
                    this.rNumeric.Checked = true;
                    Domain.Double v = value as Domain.Double;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.FloatingPointDouble;
                    return;
                }
                if (value is Domain.Decimal)
                {
                    this.rNumeric.Checked = true;
                    Domain.Decimal v = value as Domain.Decimal;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.FixedPoint;
                    return;
                }
                if (value is Domain.TinyInteger)
                {
                    this.rNumeric.Checked = true;
                    Domain.TinyInteger v = value as Domain.TinyInteger;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.Integer1Byte;
                    return;
                }
                if (value is Domain.SmallInteger)
                {
                    this.rNumeric.Checked = true;
                    Domain.SmallInteger v = value as Domain.SmallInteger;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.Integer2Byte;
                    return;
                }
                if (value is Domain.Integer)
                {
                    this.rNumeric.Checked = true;
                    Domain.Integer v = value as Domain.Integer;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.Integer4Byte;
                    return;
                }
                if (value is Domain.BigInteger)
                {
                    this.rNumeric.Checked = true;
                    Domain.BigInteger v = value as Domain.BigInteger;
                    if (v.Max != null) this.txtMaxValue.Text = v.Max.ToString();
                    if (v.Min != null) this.txtMinValue.Text = v.Min.ToString();
                    this.cmbNumericType.SelectedItem = Domain.GNumericDomains.Integer8Byte;
                    return;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.BaseDomain != null)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void rBoolean_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rBoolean)
            {
                this.txtView.Enabled = false;
                this.txtTableName.Enabled = false;
                this.txtMaximumLength.Enabled = false;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rString_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rString)
            {
                this.txtView.Enabled = false;
                this.txtTableName.Enabled = false;
                this.txtMaximumLength.Enabled = true;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rLookup_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == this.rLookup)
            {
                this.txtView.Enabled = true;
                this.txtTableName.Enabled = true;
                this.txtMaximumLength.Enabled = false;
                this.txtEnumItems.Enabled = false;
                this.txtMaxValue.Enabled = false;
                this.txtMinValue.Enabled = false;
                this.cmbNumericType.Enabled = false;
            }
            return;
        }

        private void rEnumerated_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = true;
            this.txtMaxValue.Enabled = false;
            this.txtMinValue.Enabled = false;
            this.cmbNumericType.Enabled = false;
        }

        private void rDateTime_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = false;
            this.txtMaxValue.Enabled = false;
            this.txtMinValue.Enabled = false;
            this.cmbNumericType.Enabled = false;
        }

        private void rNumeric_CheckedChanged(object sender, EventArgs e)
        {
            this.txtView.Enabled = false;
            this.txtTableName.Enabled = false;
            this.txtMaximumLength.Enabled = false;
            this.txtEnumItems.Enabled = false;
            this.txtMaxValue.Enabled = true;
            this.txtMinValue.Enabled = true;
            this.cmbNumericType.Enabled = true;
        }
    }
}